import { api_host,version} from "./appInfo";

module.exports={
	http(path, data = {}, method = 'post', showLoading = true) {
		if(!data){
			data = {}
		}
		const { platform } = uni.$u
		// if(platform === 'h5') {
		// 	data.platform = 'wechat';
		// } else if(platform === 'weixin') {
		// 	data.platform = 'wxapp';
		// }
		// if (method == 'get') {
		// 	data.timestamp = Date.parse(new Date())
		// 	// data['version'] = version
		// }
	
		if (showLoading) {
			uni.showLoading({
				title: '加载中..',
				mask: true
			})
		}
	
		let header = {
			'content-type': 'application/json; charset=utf-8',
			'Access-Control-Allow-Origin': '*',
			'app-version': version
		};
	// 	if (this.getData('token')) {
	// 		header.token = this.getData('token')
	// 	}
		
		return new Promise((resolve, reject) => {
			uni.request({
				url: api_host + path,
				method: method,
				data: data,
				header: header,
				success: (res => {
					if (res.data.msg == 200) {
						if (res.data.code == 1) {
							resolve(res.data)
						} else {
							
							uni.showModal({
								title: '提示',
								content: res.data.msg,
								showCancel: false
							})
						}
					} else {
						if (res.statusCode == 401) {
							resolve(res.data)
						} else {
							reject(res.data);
						}
						// reject(res)
					}
				}),
				fail: (res => {
					reject(res)
					// return false;
				}),
				complete: (res => {
					showLoading && uni.hideLoading()
				})
			})
		})
	},
	
}